package com.lixinlei.meetingfilm.gw.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {

    /**
     * 业务处理逻辑
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        long beginTime = System.currentTimeMillis();
        log.info("filter start ----> beginTime : {}", beginTime);

        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            long endTime = System.currentTimeMillis();
            log.info("filter end ----> endTime: [{}], cost: [{}] ms", endTime , endTime - beginTime);
        }));
    }

  /*
      request -> f1 -> f2 -> f3 -> service
      response <- f1 <- f2 <- f3 <-
   */

    /**
     * 判断 Filter 顺序的
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }

}
